package com.lhy.admin.service;

import com.lhy.admin.model.FileMapping;
import com.lhy.admin.util.AuthUtil;
import com.lhy.admin.util.QiniuUtil;
import com.lhy.commonweb.model.Page;
import com.lhy.commonweb.model.ResponseResult;
import com.lhy.commonweb.service.AbstractService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FileMappingService extends AbstractService<FileMapping, Integer> {

    @Autowired
    QiniuUtil qiniuUtil;

    @Autowired
    AuthUtil authUtil;

    public FileMappingService(@Qualifier("fileMappingMapper") Mapper<FileMapping> mapper) {
        super(mapper);
    }

    @Override
    public Page<FileMapping> query(Integer pageNumber, Integer pageSize, String sortColumns, FileMapping bean) {
        return super.queryExample(pageNumber, pageSize, sortColumns, bean);
    }

    public List<FileMapping> findParentsById(Integer parent) {
        return mapper.select(new FileMapping().setParent(parent));
    }

    /**
     * deleteFileMapping
     * <p/>
     * 删除文件，同时也回到七牛云上删除
     *
     * @param keys
     * @return void
     * @throws
     * @date 2018/7/30 14:50
     */
    public void deleteFileMapping(String... keys) {
        if (keys == null) {
            return;
        }
        qiniuUtil.delete(keys);
        Example example = new Example(FileMapping.class);
        example.createCriteria().andIn(FileMapping.FieldEnum.KEY.javaFieldName(), Arrays.asList(keys));
        mapper.deleteByExample(example);
    }

    public void deleteFileMapping(Integer id) {
        FileMapping fileMapping = mapper.selectByPrimaryKey(id);
        if (fileMapping != null) {
            this.deleteFileMapping(fileMapping.getKey());
        } else {
            throw new NullPointerException("根据文件id查找文件失败！");
        }

    }

    public FileMapping craeteFileMapping(FileMapping mapping) {
        // 创建文件时, 补充缺失的属性
        if (mapping.getFolder() == null || mapping.getFolder() == 0) {
            if (mapping.getUrl() == null) {
                if (mapping.getKey() == null) {
                    throw new NullPointerException("文件地址(url)不能为空!");
                }
                mapping.setUrl(qiniuUtil.buildUrl(mapping.getKey()));
            }
            if (mapping.getExt() == null) {
                mapping.setExt(FilenameUtils.getExtension(mapping.getName()));
            }
            mapping.setFolder(0);
        }
        Date now = new Date();
        mapping.setCreator(authUtil.getUser().getId()).setUploaded(now).setUpdated(now);
        this.mapper.insertSelective(mapping);
        return mapping;
    }


    public String uploadToken() {
        return qiniuUtil.uploadToken();
    }


}
